<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   
      <title>Examples</title>
      <link rel="stylesheet" href="styles.css" type="text/css">
      <meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
      <link rel="home" href="index.html" title="PyWordNet">
      <link rel="up" href="index.html" title="PyWordNet">
      <link rel="previous" href="index.html" title="PyWordNet">
      <link rel="next" href="download.html" title="Download">
   </head>
   <body id="examples">
      <div class="banner"><span class="projectname"><a href="http://osteele.com/projects/pywordnet/">PyWordNet</a></span></div>
      <div id="navigation">
         <ul>
            <li><a id="nav-index" href="index.html">About</a></li>
            <li><a id="nav-examples" href="examples.html">Examples</a></li>
            <li><a id="nav-download" href="download.html">Download</a></li>
            <li><a id="nav-installation" href="installation.html">Install</a></li>
            <li><a id="nav-release-notes" href="release-notes.html">Release Notes</a></li>
            <li><a id="nav-contributors" href="contributors.html">Contributors</a></li>
            <li><a id="nav-license" href="license.html">License</a></li>
         </ul>
      </div>
      <div class="section" lang="en">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title" style="clear: both"><a name="examples"></a>Examples
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <p>
            <tt class="filename">wordnet.py</tt> contains the core database access
            functions.  <tt class="filename">wntools.py</tt> contains the utility
            functions such as <tt class="function">hyponyms</tt>,
            <tt class="function">meet</tt>, <tt class="function">morphy</tt>,
            <tt class="function">closure</tt>, etc.  Importing
            <tt class="classname">wntools</tt> imports all the public functions from
            both modules:
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">
&gt;&gt;&gt; <b class="userinput"><tt>from wordnet import *</tt></b>
&gt;&gt;&gt; <b class="userinput"><tt>from wntools import *</tt></b>
</pre></td>
            </tr>
         </table>
         <p>Retrieve a <tt class="classname">Word</tt> from the
            <tt class="classname">Noun</tt> database:
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">
&gt;&gt;&gt; <b class="userinput"><tt>N['dog']</tt></b>
dog(n.)
</pre></td>
            </tr>
         </table>
         <p>&#8220;<span class="quote">Dog</span>&#8221; has six senses:
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting"><tt class="computeroutput">
&gt;&gt;&gt; <b class="userinput"><tt>N['dog'].getSenses()</tt></b>
('dog' in {noun: dog, domestic dog, Canis familiaris},
 'dog' in {noun: frump, dog}, 'dog' in {noun: dog},
 'dog' in {noun: cad, bounder, blackguard, dog, hound, heel},
 'dog' in {noun: pawl, detent, click, dog},
 'dog' in {noun: andiron, firedog, dog, dogiron})
</tt></pre></td>
            </tr>
         </table>
         <p>Bind the first-listed sense to a variable, for easier access.
            (<tt class="code">word[0]</tt> is shorthand for <tt class="code">word.getSenses()[0]</tt>.)
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">
&gt;&gt;&gt; <b class="userinput"><tt>dog = N['dog'][0]</tt></b>
&gt;&gt;&gt; <b class="userinput"><tt>dog</tt></b>
'dog' in {noun: dog, domestic dog, Canis familiaris}
</pre></td>
            </tr>
         </table>
         <p>Retrieve all the relations, of any kind, that have this sense of "dog"
            as the source.  (<tt class="code">dog.getPointers(HYPONYM)</tt> would retrieve the
            hyponyms, or names of subcategories of this sense of "dog".)
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">

&gt;&gt;&gt; <b class="userinput"><tt>dog.getPointers()</tt></b>
(hypernym -&gt; {noun: canine, canid},
 member meronym -&gt; {noun: Canis, genus Canis},
 member meronym -&gt; {noun: pack},
 hyponym -&gt; {noun: pooch, doggie, doggy, bow-wow},
 hyponym -&gt; {noun: cur, mongrel, mutt},
 hyponym -&gt; {noun: lapdog},
 hyponym -&gt; {noun: toy dog, toy},
 hyponym -&gt; {noun: hunting dog},
 hyponym -&gt; {noun: working dog},
 hyponym -&gt; {noun: dalmatian, coach dog, carriage dog},
 hyponym -&gt; {noun: basenji},
 hyponym -&gt; {noun: pug, pug-dog},
 hyponym -&gt; {noun: Newfoundland},
 hyponym -&gt; {noun: Great Pyrenees},
 hyponym -&gt; {noun: spitz},
 hyponym -&gt; {noun: griffon, Brussels griffon, Belgian griffon},
 hyponym -&gt; {noun: corgi, Welsh corgi},
 hyponym -&gt; {noun: poodle, poodle dog},
 hyponym -&gt; {noun: Mexican hairless},
 part holonym -&gt; {noun: flag})

&gt;&gt;&gt; <b class="userinput"><tt>dog.pointerTargets(MEMBER_MERONYM)</tt></b>
[{noun: Canis, genus Canis}, {noun: pack}]
</pre></td>
            </tr>
         </table>
         <p>Hypernyms of "dog", and their hypernyms, and so on until the
            links peter out.  (<tt class="code">hypernyms(dog)</tt> is a shortcut for the
            closure of this particular relationship.)
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">

&gt;&gt;&gt; <b class="userinput"><tt>closure(dog, HYPERNYM)</tt></b>
['dog' in {noun: dog, domestic dog, Canis familiaris}, {noun: canine, canid},
 {noun: carnivore}, {noun: placental, placental mammal, eutherian, eutherian
  mammal}, {noun: mammal}, {noun: vertebrate, craniate}, {noun: chordate},
 {noun: animal, animate being, beast, brute, creature, fauna}, {noun: life form,
  organism, being, living thing}, {noun: entity, something}]
&gt;&gt;&gt; cat = N['cat']
</pre></td>
            </tr>
         </table>
         <p>The <i class="glossterm">meet</i> of two items is their most subordinate common concept:
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">
&gt;&gt;&gt; <b class="userinput"><tt>meet(dog, cat[0])</tt></b>
{noun: carnivore}
&gt;&gt;&gt; <b class="userinput"><tt>meet(dog, N['person'][0])</tt></b>
{noun: life form, organism, being, living thing}
&gt;&gt;&gt; <b class="userinput"><tt>meet(N['thought'][0], N['belief'][0])</tt></b>
{noun: content, cognitive content, mental object}
</pre></td>
            </tr>
         </table>
         <p>Hyponyms of "dog" (n.) that are homophonous with verbs:</p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">
&gt;&gt;&gt; <b class="userinput"><tt>filter(lambda sense:V.get(sense.form),
           flatten1(map(lambda e:e.senses(), hyponyms(N['dog'][0]))))</tt></b>
['dog' in {noun: dog, domestic dog, Canis familiaris}, 'pooch' in {noun: pooch,
  doggie, doggy, bow-wow}, 'toy' in {noun: toy dog, toy}, 'hound' in
 {noun: hound, hound dog}, 'basset' in {noun: basset, basset hound}, 'cocker' in
 {noun: cocker spaniel, English cocker spaniel, cocker}, 'bulldog' in {noun:
  bulldog, English bulldog}]
</pre></td>
            </tr>
         </table>
         <p>The first five adjectives that are transitively SIMILAR to red (there
            are 71 in all):
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">
&gt;&gt;&gt; <b class="userinput"><tt>closure(ADJ['red'][0], SIMILAR)</tt></b>
['red' in {adjective: red, reddish, ruddy, blood-red, carmine, cerise, cherry, cherry-red, crimson, ruby, ruby-red, scarlet}, {adjective: chromatic}, {adjective: amber, brownish-yellow, yellow-brown}, {adjective: amethyst}, {adjective: aureate, gilded, gilt, gold, golden}]
</pre></td>
            </tr>
         </table>
         <p>Trace the senses of dog to the top concepts, and display the results
            in a readable form:
         </p>
         <table border="0" bgcolor="#E0E0E0">
            <tr>
               <td><pre class="programlisting">
&gt;&gt;&gt; <b class="userinput"><tt>from pprint import pprint</tt></b>
&gt;&gt;&gt; <b class="userinput"><tt>pprint(tree(N['dog'], HYPERNYM))</tt></b>
[['dog' in {noun: dog, domestic dog, Canis familiaris},
  [{noun: canine, canid},
   [{noun: carnivore},
    [{noun: placental, placental mammal, eutherian, eutherian mammal},
     [{noun: mammal},
      [{noun: vertebrate, craniate},
       [{noun: chordate},
        [{noun: animal, animate being, beast, brute, creature, fauna},
         [{noun: life form, organism, being, living thing},
          [{noun: entity, something}]]]]]]]]]],
 ['dog' in {noun: frump, dog},
  [{noun: unpleasant woman, disagreeable woman},
   [{noun: unpleasant person, disagreeable person},
    [{noun: unwelcome person, persona non grata},
     [{noun: person, individual, someone, somebody, mortal, human, soul},
      [{noun: life form, organism, being, living thing},
       [{noun: entity, something}]],
      [{noun: causal agent, cause, causal agency},
       [{noun: entity, something}]]]]]]],
 ['dog' in {noun: dog},
  [{noun: chap, fellow, lad, gent, fella, blighter, cuss},
   [{noun: male, male person},
    [{noun: person, individual, someone, somebody, mortal, human, soul},
     [{noun: life form, organism, being, living thing},
      [{noun: entity, something}]],
     [{noun: causal agent, cause, causal agency},
      [{noun: entity, something}]]]]]],
 ['dog' in {noun: cad, bounder, blackguard, dog, hound, heel},
  [{noun: villain, scoundrel},
   [{noun: unwelcome person, persona non grata},
    [{noun: person, individual, someone, somebody, mortal, human, soul},
     [{noun: life form, organism, being, living thing},
      [{noun: entity, something}]],
     [{noun: causal agent, cause, causal agency},
      [{noun: entity, something}]]]]]],
 ['dog' in {noun: pawl, detent, click, dog},
  [{noun: catch, stop},
   [{noun: restraint, constraint},
    [{noun: device},
     [{noun: instrumentality, instrumentation},
      [{noun: artifact, artefact},
       [{noun: object, physical object}, [{noun: entity, something}]]]]]]]],
 ['dog' in {noun: andiron, firedog, dog, dogiron},
  [{noun: support},
   [{noun: device},
    [{noun: instrumentality, instrumentation},
     [{noun: artifact, artefact},
      [{noun: object, physical object}, [{noun: entity, something}]]]]]]]]
</pre></td>
            </tr>
         </table>
      </div>
   </body>
</html>